# -*- coding: utf8 -*-


import json
import config
import pymongo
import datetime

from scrapy import Spider
from scrapy.http import Request
from sqlhelper import SqlHelper


class OkcoinKline(Spider):
    name = 'kline'

    def __init__(self, name=None, **kwargs):
        super(OkcoinKline, self).__init__(name, **kwargs)

        # 连接mongodb
        client = pymongo.MongoClient('47.254.26.182', 27017)
        self.db = client.bt_news

        # self.sql = SqlHelper(db_name='exchange', db_config=config)

    def start_requests(self):
        currency_pair = ['btc_usd', 'ltc_usd', 'eth_usd', 'etc_usd', 'bch_usd']

        for currency in currency_pair:
            url = 'https://www.okcoin.com/api/v1/kline.do?symbol=%s&type=1min' % currency

            yield Request(
                url = url,
                dont_filter=True,
                headers = {
                    # 'Host': 'block.cc',
                    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0',
                },
                meta = {
                    'market_pair': currency
                },
                callback = self.currency_info
            )

    def currency_info(self, response):
        body = json.loads(response.body)

        # 交易对
        kline_pair = response.meta.get('market_pair')

        # k线数据
        kline_data = str(body)

        # 入库
        data = {
            'kline_pair': kline_pair,
            'kline_data': kline_data,
        }
        # 入库mongo
        self.db.bt_kline.insert(data)
    # # close
    # def close(spider, reason):
    #     spider.sql.commit()


# 数据库
'''
CREATE TABLE `exchange_kline` (
  `kline_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'k线，自动增长，索引ID',
  `kline_data` text COMMENT 'k线数据',
  PRIMARY KEY (`kline_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
'''